using System;
using QFramework;
using UnityEngine;
using UnityEngine.InputSystem;
namespace PlatformShoot
{
    public interface IPlayerInputSystem : ISystem
    {
        void Enable();
        void Disable();
    }
    public struct DirInputEvent
    {
        public int x;
        public int y;
    }
    public struct JumpInputEvent
    {

    }
    public struct ShootInputEvent
    {
        public bool isTrigger;
    }
    /// <summary>
    /// 输入设备
    /// </summary>
    public enum E_InputDevice
    {
        Keyboard, Gamepad, Pointer
    }
    public class PlayerInputSystem : AbstractSystem, IPlayerInputSystem, GameControls.IGamePlayActions
    {
        private GameControls mControls = new GameControls();
        private DirInputEvent mInputEvent;
        private ShootInputEvent ShootInput;

        private float sensitivity = 0.2f;
        /// <summary>
        /// 当前的输入设备
        /// </summary>
        private E_InputDevice mCurrentInputDevice;
        protected override void OnInit()
        {
            mControls.GamePlay.SetCallbacks(this);
             var deviceMgr = this.GetSystem<IInputDeviceMgrSystem>();

            deviceMgr.RegisterDeviceChange<Pointer>(() => mCurrentInputDevice = E_InputDevice.Pointer);
            deviceMgr.RegisterDeviceChange<Keyboard>(() => mCurrentInputDevice = E_InputDevice.Keyboard);
            deviceMgr.RegisterDeviceChange<Gamepad>(() => mCurrentInputDevice = E_InputDevice.Gamepad);
        }
        public void OnJump(InputAction.CallbackContext context)
        {
            if (context.started)
            {
                this.SendEvent<JumpInputEvent>();
            }

        }

        public void OnMove(InputAction.CallbackContext context)
        {
            if (context.performed)
            {
               //获取方向输入 1 -1
                var input = context.ReadValue<Vector2>();
                Debug.Log(input);
                // 这一步主要考虑摇杆手感 防止摇杆误触
                switch (mCurrentInputDevice)
                {
                    case E_InputDevice.Keyboard:
                        mInputEvent.x = (int)input.x;
                        mInputEvent.y = (int)input.y;
                        break;
                    case E_InputDevice.Gamepad:
                        mInputEvent.x = Math.Abs(input.x) < sensitivity ? 0 : input.x > 0 ? 1 : -1;
                        mInputEvent.y = Math.Abs(input.y) < sensitivity ? 0 : input.y > 0 ? 1 : -1;
                        break;
                }
                this.SendEvent(mInputEvent);
            }
            else if (context.canceled)
            {
                 switch (mCurrentInputDevice)
                {
                    case E_InputDevice.Keyboard:
                        var board = Keyboard.current;

                        switch (mInputEvent.x)
                        {
                            case -1: mInputEvent.x = board.dKey.wasPressedThisFrame || board.rightArrowKey.wasPressedThisFrame ? 1 : 0; break;
                            case 1: mInputEvent.x = board.aKey.wasPressedThisFrame || board.leftArrowKey.wasPressedThisFrame ? -1 : 0; break;
                        }
                        switch (mInputEvent.y)
                        {
                            case -1: mInputEvent.y = board.wKey.wasPressedThisFrame || board.upArrowKey.wasPressedThisFrame ? 1 : 0; break;
                            case 1: mInputEvent.y = board.sKey.wasPressedThisFrame || board.downArrowKey.wasPressedThisFrame ? -1 : 0; break;
                        }
                        break;
                    default:
                        mInputEvent.x = 0;
                        mInputEvent.y = 0;
                        break;
                }
                this.SendEvent(mInputEvent);
            }
        }

        public void OnShot(InputAction.CallbackContext context)
        {
            ShootInput.isTrigger = context.ReadValueAsButton();
            this.SendEvent(ShootInput);
        }

        public void Enable()
        {
            mControls.GamePlay.Enable();
        }

        public void Disable()
        {
            mControls.GamePlay.Disable();
        }
    }
}